package com.gidgabs.modules.api.controller;

import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.dto.TbAddressDto;
import com.gidgabs.modules.api.service.TbAddressService;
import com.gidgabs.modules.api.service.TbUserService;
import com.gidgabs.modules.entity.TbAddressEntity;
import com.gidgabs.modules.entity.TbUser;
import com.gidgabs.utils.SessionDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 收货地址表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-07-02 10:07:40
 */
@Api("收货地址模块")
@RestController
@RequestMapping("/api/address")
public class TbAddressController {

    @Autowired
    private TbAddressService addressService;

    @Autowired
    private TbUserService userService;

    /**
     * 查询收货地址列表
     *
     * @param pageSize
     * @param pageNo
     * @return
     */
    @ApiOperation(value = "查询收货地址列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @GetMapping("/list")
    public ReturnJson<TbAddressDto> list(Integer pageSize, Integer pageNo) {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        return addressService.queryAdvertiseList(pageSize, pageNo);
    }

    /**
     * 根据id查询收货地址详细
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询收货地址详细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "收货地址id", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/info/{id}")
    public ReturnJson<TbAddressDto> info(@PathVariable("id") Integer id) {
        TbAddressEntity address = addressService.queryObject(id);
        return ReturnJson.success("查询成功", address.toDto());
    }

    /**
     * 保存收货地址
     */
    @ApiOperation(value = "保存收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/save")
    public ReturnJson<String> save(@ModelAttribute TbAddressDto address) {
        address.setUserId(SessionDataUtil.getCurrentUserId());
        addressService.save(address.toEntity());
        return ReturnJson.success("操作成功");
    }

    /**
     * 修改 为默认收货地址
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "修改 为默认收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "收货地址id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/update")
    public ReturnJson<String> update(Integer id) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
        //查询该用户的默认收货地址
        TbAddressEntity tbAddressEntity = new TbAddressEntity();
        tbAddressEntity.setUserId(user.getUserId());
        tbAddressEntity.setIsDefault(0);
        TbAddressEntity addressEntity = addressService.selectTbAddressObjSingle(tbAddressEntity);
        //判断是否有默认地址 如果有设置就修改为非默认 反之就直接设置默认收货地址
        if (addressEntity != null) {
            addressEntity.setIsDefault(1);
            addressService.update(addressEntity);
        }
        TbAddressEntity addressEntity1 = new TbAddressEntity();
        addressEntity1.setId(id);
        addressEntity1.setIsDefault(0);
        addressService.update(addressEntity1);
        return ReturnJson.success("操作成功");
    }

    /**
     * 删除收货地址 可批量
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除收货地址 可批量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "收货地址id数组", dataType = "int[]", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/delete")
    public ReturnJson<String> delete(Integer[] ids) {
        addressService.deleteBatch(ids);
        return ReturnJson.success("操作成功");
    }

}
